package com.ruoyi.vbze.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.vbze.domain.Plant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vbze.mapper.SamplePointMapper;
import com.ruoyi.vbze.domain.SamplePoint;
import com.ruoyi.vbze.service.ISamplePointService;

/**
 * 采样点Service业务层处理
 * 
 * @author vbze
 * @date 2023-02-27
 */
@Service
public class SamplePointServiceImpl implements ISamplePointService 
{

    private static final Logger log = LoggerFactory.getLogger(SamplePointServiceImpl.class);
    @Autowired
    private SamplePointMapper samplePointMapper;

    /**
     * 查询采样点
     * 
     * @param id 采样点主键
     * @return 采样点
     */
    @Override
    public SamplePoint selectSamplePointById(Long id)
    {
        return samplePointMapper.selectSamplePointById(id);
    }

    /**
     * 查询采样点列表
     * 
     * @param samplePoint 采样点
     * @return 采样点
     */
    @Override
    public List<SamplePoint> selectSamplePointList(SamplePoint samplePoint)
    {
        return samplePointMapper.selectSamplePointList(samplePoint);
    }

    /**
     * 新增采样点
     * 
     * @param samplePoint 采样点
     * @return 结果
     */
    @Override
    public int insertSamplePoint(SamplePoint samplePoint)
    {
        return samplePointMapper.insertSamplePoint(samplePoint);
    }

    /**
     * 修改采样点
     * 
     * @param samplePoint 采样点
     * @return 结果
     */
    @Override
    public int updateSamplePoint(SamplePoint samplePoint)
    {
        return samplePointMapper.updateSamplePoint(samplePoint);
    }

    /**
     * 批量删除采样点
     * 
     * @param ids 需要删除的采样点主键
     * @return 结果
     */
    @Override
    public int deleteSamplePointByIds(Long[] ids)
    {
        return samplePointMapper.deleteSamplePointByIds(ids);
    }

    /**
     * 删除采样点信息
     * 
     * @param id 采样点主键
     * @return 结果
     */
    @Override
    public int deleteSamplePointById(Long id)
    {
        return samplePointMapper.deleteSamplePointById(id);
    }





    /**
     * 导入采样点数据
     *
     * @param samplePointList 采样点数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importSamplePoint(List<SamplePoint> samplePointList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(samplePointList) || samplePointList.size() == 0)
        {
            throw new ServiceException("导入采样点数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
//        String password = configService.selectConfigByKey("sys.user.initPassword");
        for (SamplePoint samplePoint : samplePointList)
        {
            try
            {
                // 验证是否存在这个地块
//                SysUser u = userMapper.selectUserByUserName(block.getUserName());
                SamplePoint s = samplePointMapper.selectSamplePointByLabelAndPointName(samplePoint.getLabel(), samplePoint.getPointName());
                if (StringUtils.isNull(s))
                {
//                    BeanValidators.validateWithException(validator, block);
//                    block.setPassword(SecurityUtils.encryptPassword(password));
                    samplePoint.setCreateBy(operName);
//                    samplePoint.setLastEditor(operName);
                    samplePoint.setUpdateBy(operName);
                    this.insertSamplePoint(samplePoint);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、采样点 " + samplePoint.getLabel() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
//                    BeanValidators.validateWithException(validator, block);
//                    checkUserAllowed(block);
//                    checkUserDataScope(block.getUserId());
//                    block.setUpdateBy(operName);
                    samplePoint.setUpdateBy(operName);
//                    samplePoint.setLastEditor(operName);
                    this.updateSamplePoint(samplePoint);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、采样点 " + samplePoint.getLabel() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、采样点 " + samplePoint.getLabel() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、采样点 " + samplePoint.getLabel() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
